package pay

import (
	"context"

	"github.com/agui-coder/simple-admin-pay-rpc/internal/svc"
	"github.com/agui-coder/simple-admin-pay-rpc/types/pay"
	"github.com/agui-coder/simple-admin-pay-rpc/utils/errorhandler"
	"github.com/suyuan32/simple-admin-common/utils/pointy"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPancakebotOrderListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetPancakebotOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPancakebotOrderListLogic {
	return &GetPancakebotOrderListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetPancakebotOrderListLogic) GetPancakebotOrderList(in *pay.PancakebotOrderListReq) (*pay.PancakebotOrderListResp, error) {
	page, err := l.svcCtx.DB.PancakebotOrder.Query().Page(l.ctx, in.Page, in.PageSize)
	if err != nil {
		return nil, errorhandler.DefaultEntError(l.Logger, err, in)
	}
	infos := make([]*pay.PancakebotOrderInfo, len(page.List))
	for i, demoOrder := range page.List {
		infos[i] = &pay.PancakebotOrderInfo{
			Id:              &demoOrder.ID,
			CreatedAt:       pointy.GetPointer(demoOrder.CreatedAt.UnixMilli()),
			UpdatedAt:       pointy.GetPointer(demoOrder.UpdatedAt.UnixMilli()),
			UserId:          &demoOrder.UserID,
			ComboId:         &demoOrder.ComboID,
			ComboName:       &demoOrder.ComboName,
			ComboIngredient: &demoOrder.ComboIngredient,
			Price:           &demoOrder.Price,
			PayStatus:       &demoOrder.PayStatus,
			PayOrderId:      &demoOrder.PayOrderId,
			PayTime:         pointy.GetPointer(demoOrder.PayTime.UnixMilli()),
			PayChannelCode:  &demoOrder.PayChannelCode,
			PayRefundId:     &demoOrder.PayRefundID,
			RefundPrice:     &demoOrder.RefundPrice,
			RefundTime:      pointy.GetPointer(demoOrder.RefundTime.UnixMilli()),
		}
	}
	return &pay.PancakebotOrderListResp{
		Data:  infos,
		Total: page.PageDetails.Total,
	}, nil
}
